/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUIv2;

import javax.swing.JOptionPane;

import jsonObjects.Attributes;
import jsonObjects.Profile;
import serverHandling.SessionObjects;

public class AttributesPropertiesGUI extends javax.swing.JPanel {

    private static final long serialVersionUID = -4015357288544920793L;
    public AttributesPropertiesGUI(String profId) {
        attributes = SessionObjects.getProfileAttributes(profId);

        initComponents();
    }

    @SuppressWarnings("serial")
    private void initComponents() {
        Object[][] futureMenuItems;
        if (attributes != null) {
            futureMenuItems = new Object[attributes.length][attributes.length];
            for (int i = 0; i < attributes.length; i++) {
                futureMenuItems[i][0] = attributes[i].getName();
            }
        } else
            futureMenuItems = new Object[0][0];

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        nextButton = new javax.swing.JButton();
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cancelButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                futureMenuItems,
                new String[]{
                        "Name"
                }
        ) {
            boolean[] canEdit = new boolean[]{
                    false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        nextButton.setText("View attributes");

        cancelButton.setText("Return");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(cancelButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(nextButton)))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(nextButton)
                                        .addComponent(cancelButton))
                                .addContainerGap())
        );
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        if (jTable1.getSelectedRowCount() == 1) {
            int row = jTable1.getSelectedRow();
            int column = jTable1.getSelectedColumn();
            String urObjctInCell = (String) jTable1.getValueAt(row, column);
            final Profile profTmp = SessionObjects.getProfileByName(urObjctInCell);

            if (profTmp == null)
                JOptionPane.showMessageDialog(null,"Nie odnaleziona atrybutow dla wybranego profilu!");

            else {
                this.setVisible(false);
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        new AttributesPropertiesGUI(profTmp.getId()).setVisible(true);
                    }
                });
            }
        } else {
            JOptionPane.showMessageDialog(null, "Nalezy wybrac profil!");
        }
    }

    javax.swing.JButton cancelButton;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JTable jTable1;
    javax.swing.JButton nextButton;
    Attributes[] attributes;

}
